package 线性结构.util;

/**
 * 双向链表
 */
public class DoubleLinkedList<E> implements IList<E> {

    private int size;
    private Node<E> first = null;
    private Node<E> last = null;

    private static class Node<E> {
        E element;
        Node<E> next;
        Node<E> prev;

        public Node(Node<E> prev, E element, Node<E> next) {
            this.element = element;
            this.next = next;
            this.prev = prev;
        }

        public Node(E element, Node<E> next) {
            this(null, element, next);
        }

        @Override
        public String toString() {
            StringBuilder builder = new StringBuilder();
            if (prev != null) {
                builder.append(prev.element);
            } else {
                builder.append("null");
            }
            builder.append("_").append(element).append("_");
            if (next != null) {
                builder.append(next.element);
            } else {
                builder.append("null");
            }

            return builder.toString();
        }
    }

    @Override
    public void add(E element) {
        add(size, element);
    }

    @Override
    public E remove(int index) {
        rangeCheck(index);
        Node<E> curNode = getNodeByIndex(index);
        Node<E> next = curNode.next;
        Node<E> prev = curNode.prev;

        //删除首节点
        if (prev == null) {
            first = next;
        } else {
            prev.next = next;
        }

        //删除尾节点
        if (next == null) {
            last = prev;
        } else {
            next.prev = prev;
        }


        size--;
        return (E) curNode.element;
    }

    @Override
    public void add(int index, E element) {

        //往最后插入
        if (index == size) {
            Node<E> oldLast = this.last;
            last = new Node<E>(oldLast, element, null);
            //一开始插入
            if (oldLast == null) {
                first = last;
            } else {
                oldLast.next = last;
            }
        } else {
            Node<E> curNode = getNodeByIndex(index);
            Node<E> prev = curNode.prev;
            Node<E> newNode = new Node<E>(prev, element, curNode);

            //首节点插入
            if (prev == null) {
                first = newNode;
            } else {
                prev.next = newNode;
            }

            curNode.prev = newNode;
        }


        size++;
    }

    @Override
    public void set(int index, E element) {
        getNodeByIndex(index).element = element;
    }

    @Override
    public E get(int index) {
        return null;
    }

    @Override
    public void clear() {
        size = 0;
        first = null;
        last = null;
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public int indexOf(E element) {
        if (element == null) {
            Node node = first;
            for (int i = 0; i < size; i++) {
                if (node.element == null) return i;
                node = node.next;
            }
        } else {
            Node node = first;
            for (int i = 0; i < size; i++) {
                if (node.element.equals(element)) return i;
                node = node.next;
            }
        }
        return -1;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public boolean contains(E element) {
        return indexOf(element) != -1;
    }

    private void rangeCheck(int index) {
        if (index < 0 || index > size) {
            throw new ArrayIndexOutOfBoundsException("size" + size + " index:" + index);
        }
    }

    /**
     * 根据下标找到对应的节点
     *
     * @param index
     * @return
     */
    private Node<E> getNodeByIndex(int index) {
        rangeCheck(index);
        Node<E> node = null;
        if (index < size >> 1) {
            node = first;
            for (int i = 0; i < index; i++) {
                node = node.next;
            }
        } else {
            node = last;
            for (int i = size - 1; i > index; i--) {
                node = node.prev;
            }
        }
        return node;
    }

    @Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("size:").append(size).append(" [");
        for (int i = 0; i < size; i++) {
            if (i != 0) {
                stringBuilder.append(",");
            }
            stringBuilder.append(getNodeByIndex(i));
        }
        stringBuilder.append("]");
        return stringBuilder.toString();
    }
}
